<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

class Visit extends Backend
{
    protected $model = null;
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->model = model("Business.Visit");
        $this->Businessmodel = model("Business.Business");
        $nameList = $this->Businessmodel->select();
        $nameData = [];
        foreach ($nameList as $val) {
            $nameData[$val['id']] = $val['nickname_text'];
        }
        $this->assign([
            'nameData' => $nameData,
        ]);
    }
    public function index($ids = NULL)
    {
        // var_dump($ids);
        // exit;
        // if()
        if ($ids != null) {
            if ($this->request->isAjax()) {
                //接收参数
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();

                $total = $this->model
                    ->with(['business', 'admin'])
                    ->where($where)
                    ->where('busid', $ids)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['business', 'admin'])
                    ->where($where)
                    ->where('busid', $ids)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

                //返回json数据
                $result = ['total' => $total, 'rows' => $list];

                return json($result);
            }
        } else {
            // var_dump(null);
            if ($this->request->isAjax()) {
                //接收参数
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();

                $total = $this->model
                    ->with(['business', 'admin'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['business', 'admin'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

                // exit;
                //返回json数据
                $result = ['total' => $total, 'rows' => $list];

                return json($result);
            }

        }
        return $this->view->fetch();
    }

    // 添加
    public function add($ids = NULL)
    {
        $i=isset($ids) ? $ids : 0;
        // var_dump($ids);
        // exit;
        if ($i==0) {
            if($this->request->isPost())
            {
                $params=$this->request->param('row/a');
                $params['adminid'] = $this->auth->id;
                $result = $this->model->validate('common/business/Visit')->save($params);
                if ($result === FALSE) {
                    $this->error($this->model->getError());
                    exit;
                } else {
                    $this->success();
                    exit;
                }
            }
            $this->assign([
                'ids' => 'null',
            ]);
        }
        else
        {
            // var_dump($ids);
            if ($this->request->isAjax()) {
                $params = $this->request->param('row/a');
                $params['busid'] = $ids;
                $params['adminid'] = $this->auth->id;
                // var_dump($adminid);
                // exit;
                $result = $this->model->validate('common/business/Visit')->save($params);
                if ($result === FALSE) {
                    $this->error($this->model->getError());
                    exit;
                } else {
                    $this->success();
                    exit;
                }
            }
            $this->assign([
                'ids' => $ids,
            ]);
            
        }
        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = NULL)
    {
        $row = $this->model->find($ids);
        $this->assign([
            'row' => $row,
        ]);
        if ($this->request->isAjax()) {
            $params = $this->request->param('row/a');
            $params['id'] = $ids;
            $result = $this->model->isUpdate(true)->save($params);
            if ($result === FALSE) {
                $this->error($this->model->getError());
                exit;
            } else {
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = NULL)
    {
        $ids = empty($ids) ? [] : explode(',', $ids);
        $row = $this->model->column('id');
        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('No results were found'));
                exit;
            }
        }
        $result = $this->model->destroy($ids);
        if ($result === FALSE) {
            $this->error($this->model->getError());
            exit;
        } else {
            $this->success();
            exit;
        }
    }

}